﻿using DataDefinition;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LLTreeManagementApplication.Data
{
    enum CalculationType
    {
        None = 0,
        FileSize = 1,
        FileCount = 2,       
    }

    class DataComplexityCalculation
    {
        //private const int LISTTHRESHOLD = 5000;
        //private const long MAXFILEZIZE = 262144000; //(250MB)
        //private const long MAXSITECOLLECTIONSIZE = 53687091200; //(50GB)

        public void CalculateTreeNodeSize(TreeView tree)
        {
            foreach (TreeNode node in tree.Nodes)
            {
                CalculateComplexity(node, CalculationType.FileCount);
                CalculateComplexity(node, CalculationType.FileSize);
                //CalculateComplexity(node, CalculationType.MaxFileSizeExceeded, coloring);
                //CalculateItemThreshold(node, coloring);
            }
        }

        //public void CheckGovernanceRules(TreeView tree, bool coloring = false)
        //{
        //    foreach (TreeNode node in tree.Nodes)
        //    {
        //        CalculateComplexity(node, CalculationType.FileCount);
        //        CalculateComplexity(node, CalculationType.FileSize);
        //        CalculateItemSizeExceeded(node, coloring);
        //        CalculateItemThreshold(node, coloring);
        //    }
        //}

        //private int CalculateItemThreshold(TreeNode node, bool coloring)
        //{
        //    int value = 0;

        //    DataNode currDataNode = node.Tag as DataNode;

        //    if (currDataNode.SharePointType == SharePointType.DocLibrary)
        //    {
        //        foreach (TreeNode child in node.Nodes)
        //        {
        //            var data = child.Tag as DataNode;

        //            if (data.SharePointType == SharePointType.Document ||
        //                data.SharePointType == SharePointType.Item)
        //            {
        //                value++;
        //            }
        //        }

        //        if (value > LISTTHRESHOLD)
        //        {
        //            currDataNode.ItemThresholdExceededCount = 1;
        //            value = 1;
        //            if (coloring)
        //            {
        //                if (currDataNode.Deletable == false)
        //                {
        //                    node.ForeColor = Color.Red;
        //                }
        //                else
        //                {
        //                    node.ForeColor = Color.DarkRed;
        //                }
        //            }
        //        }
        //        else
        //        {
        //            value = 0;
        //        }
        //    }
        //    else
        //    {
        //        foreach (TreeNode child in node.Nodes)
        //        {
        //            value += CalculateItemThreshold(child, coloring);
        //        }
        //        currDataNode.ItemThresholdExceededCount = value;
        //    }
        //    return value;
        //}

        //private long CalculateItemSizeExceeded(TreeNode node, bool coloring)
        //{
        //    long value = 0;
        //    #region Recursive tree iteration
        //    foreach (TreeNode child in node.Nodes)
        //    {
        //        value += CalculateItemSizeExceeded(child, coloring);
        //    }

        //    DataNode currDataNode = node.Tag as DataNode;

        //    if (currDataNode != null)
        //    {
        //        if (currDataNode.FileSize > MAXFILEZIZE)
        //        {
        //            value++;
        //        }
        //        currDataNode.ItemSizeExceededCount = (int)value;
        //        if (coloring && currDataNode.ItemSizeExceededCount > 0)
        //        {
        //            if (currDataNode.Deletable == false)
        //            {
        //                node.ForeColor = Color.Red;
        //            }
        //            else
        //            {
        //                node.ForeColor = Color.DarkRed;
        //            }
        //        }
        //    }

        //    return value;
        //    #endregion
        //}

        private long CalculateComplexity(TreeNode node, CalculationType type)
        {
            long value = 0;
            #region Recursive tree iteration
            foreach (TreeNode child in node.Nodes)
            {
                value += CalculateComplexity(child, type);
            }

            DataNode currDataNode = node.Tag as DataNode;

            if (currDataNode != null)
            {
                switch (type)
                {
                    case CalculationType.FileCount:
                        currDataNode.NodeCount = (int)value;
                        value = currDataNode.ItemCount + currDataNode.NodeCount;
                        break;
                    case CalculationType.FileSize:
                        currDataNode.NodeSize = value;
                        value = currDataNode.FileSize + currDataNode.NodeSize;
                        break;
                }
            }

            return value;
            #endregion
        }

        // source : http://dotnet-snippets.de/snippet/byte-groessenangaben-als-string-formatieren-kb-mb-gb/1304
        public static string ToFuzzyByteString(long bytes)
        {
            double s = bytes;
            string[] format = new string[]
                  {
                      "{0} bytes", "{0} KB",  
                      "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB"
                  };

            int i = 0;

            while (i < format.Length && s >= 1024)
            {
                s = (long)(100 * s / 1024) / 100.0;
                i++;
            }
            return string.Format(format[i], s);
        }
    }
}
